//
//  MineTableViewController.m
//  FreshBee
//
//  Created by Goofy on 15/10/2016.
//  Copyright © 2016 iOS013. All rights reserved.
//

#import "MineTableViewController.h"
#import "CKMineTableViewCell.h"
#import "CKOrderTableViewController.h"
#import "CKOrderStatusAndDetailVC.h"
#import "CKTicketVC.h"
#import "SuggestViewController.h"
#import "HelpViewController.h"
#import "SeetingTableViewController.h"

@interface MineTableViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,weak) UIImageView *headView;
@property (nonatomic,weak) UITableView *tableView;
@property (nonatomic,weak) UIImageView *avatar;
@property (nonatomic,weak) UILabel *phoneNum;
@property (nonatomic,weak) UIButton *setBtn;


@end

@implementation MineTableViewController

#pragma mark
#pragma mark 懒加载子控件:
- (UITableView *)tableView{
    if(!_tableView){
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView = tableView;
        tableView.delegate = self;
        tableView.dataSource = self;
        [self.view addSubview:_tableView];
        [tableView setBackgroundColor: [UIColor colorWithWhite:0.9 alpha:1]];
        tableView.sectionHeaderHeight = 0;
        tableView.sectionFooterHeight = 10;
        tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.1)];
        [tableView registerNib:[UINib nibWithNibName:@"CKMineTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
        tableView.allowsSelection = YES;
    }
    return _tableView;
}

- (UIImageView *)headView{
    
    if(!_headView)
    { 
        UIImageView *view = [[UIImageView alloc]init];
        _headView = view;
        [_headView setBackgroundColor:[UIColor redColor]];
        [self.view addSubview:_headView];
        [view setImage:[UIImage imageNamed:@"v2_my_avatar_bg"]];
        
        
    }
    return _headView;
}
//头像:
- (UIImageView *)avatar{
    if(!_avatar)
    {
        UIImageView *avatar = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"v2_my_avatar"]];
        _avatar = avatar;
        [self.view addSubview:avatar];
    }
    return _avatar;
}
//手机号:
- (UILabel *)phoneNum{
    if(!_phoneNum){
        UILabel *label = [[UILabel alloc]init];
        _phoneNum = label;
        [label setText:@"15538385438"];
        [label setTextColor:[UIColor whiteColor]];
        [self.view addSubview:label];
    }
    return _phoneNum;
}
//设置按钮:
- (UIButton *)setBtn{
    if(!_setBtn){
        UIButton *btn = [[UIButton alloc]init];
        _setBtn = btn;
        [self.view addSubview:btn];
        [btn setImage:[UIImage imageNamed:@"v2_my_settings_icon"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(setBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _setBtn;
}
- (void)setBtnClick:(UIButton *)btn{
    NSLog(@"设置按钮 被点击");
    SeetingTableViewController *setView = [[SeetingTableViewController alloc]init];
    [self.navigationController pushViewController:setView animated:YES];
}


#pragma mark
#pragma mark viewDidLoad :
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view setBackgroundColor: [UIColor whiteColor]];
    
    [self setupUI];

}
- (void)setupUI{
    

}

#pragma mark
#pragma mark 设置子控件约束:
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    double screenWidth = [UIScreen mainScreen].bounds.size.width;
    //头view：
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.trailing.leading.equalTo(self.view);
        make.height.equalTo(@(screenWidth/2));
    }];
    //tableView:
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.headView.mas_bottom);
        make.bottom.equalTo(self.view);

    }];
    
    //avatar:
    [self.avatar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.headView.mas_centerX);
        make.centerY.equalTo(self.headView.mas_centerY).offset(-10);
    }];
    //手机号:
    [self.phoneNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.headView.mas_centerX);
        make.centerY.equalTo(self.headView.mas_centerY).offset(40);
    }];
    //setBtn:
    [self.setBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headView).offset(20);
        make.trailing.equalTo(self.headView).offset(-10);
    }];
}



- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    self.tabBarController.tabBar.hidden = NO;
}


#pragma mark - Table view data source------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section == 0 || section == 2){
        return 1;
    }
    return 2;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0){
        
        CKMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.btnClickBlock = ^(int tag){
            //点击事件:
            NSLog(@"点击按钮%d",tag);
            if (tag == 1){
                CKOrderTableViewController *tbv = [[CKOrderTableViewController alloc]init];
                [self.navigationController pushViewController:tbv animated:YES];
            }
            if (tag == 2){
                CKTicketVC *tbv = [[CKTicketVC alloc]init];
               
                [self.navigationController pushViewController:tbv animated:YES];
            }
            if(tag == 3){
                NSLog(@"我的消息点击");
                CKOrderStatusAndDetailVC *tbv = [[CKOrderStatusAndDetailVC alloc]initWithViewType:kViewTypeMyMessage];
                [self.navigationController pushViewController:tbv animated:YES];
            }
            
        };
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    UITableViewCell *cell2 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    [cell2 setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    [cell2 setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 0)];
    if(indexPath.section == 1 && indexPath.row == 0){
        [cell2 setImage:[UIImage imageNamed:@"v2_my_address_icon"]];
        [cell2 setText:@"我的收货地址"];
    }
    if(indexPath.section == 1 && indexPath.row == 1){
        [cell2 setImage:[UIImage imageNamed:@"icon_mystore"]];
        [cell2 setText:@"我的店铺"];
    }
    if(indexPath.section == 2){
        [cell2 setImage:[UIImage imageNamed:@"v2_my_share_icon"]];
        [cell2 setText:@"把爱鲜蜂分享给朋友"];
    }
    if(indexPath.section == 3 && indexPath.row == 0){
        [cell2 setImage:[UIImage imageNamed:@"v2_my_serviceonline_icon"]];
        [cell2 setText:@"客服帮助"];
    }
    if(indexPath.section == 3 && indexPath.row == 1){
        [cell2 setImage:[UIImage imageNamed:@"v2_my_feedback_icon"]];
        [cell2 setText:@"意见反馈"];
    }
    return cell2;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.section == 0 ? 60 : 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section == 1 && indexPath.row == 0){
        NSLog(@"跳转到收货地址");
        
    }
    if(indexPath.section == 3 && indexPath.row == 0){
        NSLog(@"跳转到客服帮助");
        SuggestViewController *sug = [[SuggestViewController alloc]init];
        [self.navigationController pushViewController:sug animated:YES];
        
    }
    if(indexPath.section == 3 && indexPath.row == 1){
        NSLog(@"跳转到意见反馈");
        HelpViewController *help = [[HelpViewController alloc]init];
        [self.navigationController pushViewController:help animated:YES];
    }
}

@end
